package com.wikidot.designpatterns.composite.alternative1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Composite implements Component {

	protected String name;

	public Composite(String name) {
		this.name = name;
	}

	@Override
	public void businessOperation() {
		System.out.println(this.name
				+ " performing business operation for composite node.");
		// Call business method for all descendants
		for (Component descendant : this.composite) {
			descendant.businessOperation();
		}
	}

	/*
	 * Composite management logic - methods for manipulating composite structure
	 * (adding, removing, listing, ...). All inherited classes will dispose
	 * these methods - even leaf elements.
	 */
	protected Collection<Component> composite = new ArrayList<Component>();

	public final void addComponent(Component c) {
		composite.add(c);
	}

	public final void removeComponent(Component c) {
		composite.remove(c);
	}

	public final Iterator<Component> listComponents() {
		return composite.iterator();
	}
}
